package org.jeecgframework.web.system.controller.core;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.tag.vo.datatable.SortDirection;
import org.jeecgframework.web.system.pojo.base.CbsWxUserBindEntity;
import org.jeecgframework.web.system.pojo.base.TSBaseUser;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.pojo.base.ZyContactsEntity;
import org.jeecgframework.web.system.service.SystemService;



/**   
 * @Title: Controller
 * @Description: 微信会员验证
 * @author onlineGenerator
 * @date 2016-08-26 18:51:57
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/cbsWxUserBindController")
public class CbsWxUserBindController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CbsWxUserBindController.class);

	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 微信会员验证列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "cbsWxUserBind")
	public ModelAndView cbsWxUserBind(HttpServletRequest request) {
		return new ModelAndView("com/zybk/cbs_wx_user_bind/cbsWxUserBindList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(CbsWxUserBindEntity cbsWxUserBind,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(CbsWxUserBindEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, cbsWxUserBind, request.getParameterMap());
		try{
		//自定义追加查询条件
			cq.addOrder("approvalStatus", SortDirection.desc);
			cq.addOrder("createDate", SortDirection.desc);
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	@RequestMapping(params="authWxUser")
	public ModelAndView authWxUser(HttpServletRequest request,String id){
		CbsWxUserBindEntity bind = systemService.get(CbsWxUserBindEntity.class, id);
		TSBaseUser user = systemService.findUniqueByProperty(TSBaseUser.class, "userName", bind.getUserName());
		if(StringUtil.isNotEmpty(user)){
			ZyContactsEntity contacts = systemService.findUniqueByProperty(ZyContactsEntity.class, "tsBaseUser.id", user.getId());
//			contacts.setEmail(bind.getEmail());
//			contacts.setPosition(bind.getDepartName());
			request.setAttribute("zyContactsPage", contacts);
			request.setAttribute("wxBindId", id);
            request.setAttribute("cinemaName",bind.getCinemaName());
		}
		return new ModelAndView("com/zybk/zy_contacts/zyContacts-update-wx");
	}

	
	/**
	 * 审批用户认证结果
	 * @param id 
	 * @param type 1.同意 2.不同意
	 * @return
	 */
	@RequestMapping(params="doUpdateStataus")
	@ResponseBody
	public AjaxJson doUpdateStataus(String id,String type){
		AjaxJson j = new AjaxJson();
		message = "微信认证审批成功";
		try {
			CbsWxUserBindEntity userBind = systemService.get(CbsWxUserBindEntity.class, id);
			userBind.setApprovalStatus(type);
			systemService.saveOrUpdate(userBind);
			if("1".equals(type)){
				List<TSBaseUser> userList = systemService.findHql("from TSBaseUser where openId = ?", userBind.getOpenId());
				if(userList.size() > 0){
					userList.get(0).setAuthenticationLevel("2");
					systemService.saveOrUpdate(userList.get(0));
				}
			}
			systemService.addLog(message,Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "微信认证审批失败";
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * @author xiul
	 * 2017-5-3 上午11:52:36
	 * Description 跳转 编辑备注页面
	 */
	@RequestMapping(params="editRemark")
	public ModelAndView editRemark(HttpServletRequest request){
		String id = oConvertUtils.getString(request.getParameter("id"));
		if(!id.equals("")){
			CbsWxUserBindEntity wubind = systemService.get(CbsWxUserBindEntity.class, id);
			request.setAttribute("wubind", wubind);
		}
		return new ModelAndView("com/zybk/cbs_wx_user_bind/cbsWxUserBindRemark");
	}
	
	/**
	 * @author xiul
	 * 2017-5-3 上午11:53:24
	 * Description 保存编辑的备注
	 */
	@ResponseBody
	@RequestMapping(params="saveEidtedRemark")
	public AjaxJson saveEidtedRemark(HttpServletRequest request,@RequestParam(value="remark",defaultValue="")String remark){
		AjaxJson j = new AjaxJson();
		try {
			String id = oConvertUtils.getString(request.getParameter("id"));
			CbsWxUserBindEntity wubind = systemService.get(CbsWxUserBindEntity.class, id);
			if(null != wubind){
				wubind.setRemark(remark);
				systemService.saveOrUpdate(wubind);
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setSuccess(false);
			j.setMsg("保存备注失败");
		}
		return j;
	}
	
}
